Skip to main content

Chapter 9 - Terraform Resource Syntax, Behavior, State

Basic intro to syntax

Syntax introduction


resource "azurerm_resource_group" "myrg1" {

}
  • Resource block
  • resource type
  • Resource local name

Resource type and name together serve as the identifier.

Create TF Config for VNet


You should be able to CTRL+Space this out and create the stub code for what you need to build. You can reference this on the terraform provider registry for the virtual network.

# Create Vnet
resource "azurerm_virtual_network" "vnet1" {
address_space = [ "10.0.0.0/16" ]
location = azurerm_resource_group.myrg.location
name = "myvnet01"
resource_group_name = azurerm_resource_group.myrg.name
tags = {
"managedby" = "Solomon"
}

}

Note two different types of brackets here [] and {}

  • [] = List
  • = Map

What is the difference between a block and a map. Maps are assigned with an = sign.

Run a terraform init, validate, plan, apply and destroy to test this.

Create Subnet, Public IP, NIC


Notice that the ip_configuration BLOCK is not a map.

The finished code should look something like this:

# Create Vnet
resource "azurerm_virtual_network" "myvnet1" {
address_space = [ "10.0.0.0/16" ]
location = azurerm_resource_group.myrg.location
name = "myvnet-1"
resource_group_name = azurerm_resource_group.myrg.name
tags = {
"managedby" = "Solomon"
}

}
# Create Subnet
resource "azurerm_subnet" "mysubnet1" {
name = "mysn-1"
resource_group_name = azurerm_resource_group.myrg.name
virtual_network_name = azurerm_virtual_network.myvnet1.name
address_prefixes = ["10.0.1.0/24"]
}
# Create Public IP
resource "azurerm_public_ip" "mypublicip" {
name = "mypublicip-1"
resource_group_name = azurerm_resource_group.myrg.name
location = azurerm_resource_group.myrg.location
allocation_method = "Dynamic"
tags = {
"managedby" = "Solomon"
}

}
#Create NIC
resource "azurerm_network_interface" "mynic1" {
name = "mynic-1"
location = azurerm_resource_group.myrg.location
resource_group_name = azurerm_resource_group.myrg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.mysubnet1.id
private_ip_address_allocation = "Dynamic"
private_ip_address = azurerm_public_ip.mypublicip.id
}

}
  • Walk through this code, line by line and try to understand everything that it is doing.
  • Look for differences in blocks {} and maps = {}, how a list [] is used.
  • Look at how you can chain resources together.
  • Look how you can create one RG and then assign all of the resources to that RG without hardcoding them

Terraform Resource Behavior


  • Create - create the code while the resource has not yet been created image.png
  • Destroy - remove the resource code from the tf file and it still exists in the state
    image.png
  • Update in place - changes to a resource
    image.png
  • Destroy and recreate - destroy and recreate resource.
    image.png image.png

State


What is the state? State is just a representation of what your infrastructure looks like in the cloud.

Where is it stored? On your laptop or in a shared place such as an S3 bucket.

What is IN the state? Blocks of all of the metadata of the resources IN PLAIN TEXT, not just what you've configured in the .tf file.

Do you edit the state file? No. Don't. Please.

Resource behavior - update in place, destroy-recreate, destroy.


See the images above for those actions.

Understand Terraform Desired States


Terraform desired state is the configuration of the .tf files, the current state is the real world resources. When your desired state matches your current state, you get a No changes, infra is up to date.

Drift


Drift occurs when your desired state is different than your real state.

Driftctl


https://docs.driftctl.com/0.40.0